package leeon.kaixin.wap.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import leeon.kaixin.wap.util.HttpUtil;
import leeon.mobile.BBSBrowser.ContentException;
import leeon.mobile.BBSBrowser.NetworkException;
import leeon.mobile.BBSBrowser.actions.HttpConfig;
import leeon.mobile.BBSBrowser.utils.HTTPUtil;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

public class DiaryAction {
	
	
	public static Map<String, String> prePostDiary(String verify) throws NetworkException {
		String url = HttpUtil.KAXIN_DAIRY_WRITE_URL + HttpUtil.KAIXIN_PARAM_VERIFY + verify;
		HttpClient client = HttpConfig.newInstance();
		HttpGet get = new HttpGet(url);

		HttpUtil.setHeader(get);
		try {
			HttpResponse response = client.execute(get);
			String html = HTTPUtil.toString(response.getEntity());
			//<select name="classid" class="f14"><option value="6924352"  >随笔日记</option>
			//<option value="6924353"  >我的文章</option><option value="6924354"  >网络转载</option></select>
			return PictureAction.dealSelectOptions(html, "name=\"classid\"");
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
	}
	
	public static void postDiary(String title, String content, String classid) throws ContentException, NetworkException {
//		POST http://www.kaixin001.com/diary/write_submit.php HTTP/1.1
//		Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
//		Referer: http://www.kaixin001.com/diary/write.php?classid=0
//		Accept-Language: zh-cn
//		Content-Type: application/x-www-form-urlencoded
//		UA-CPU: x86
//		Accept-Encoding: gzip, deflate
//		User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
//		Connection: Keep-Alive
//		Content-Length: 721
//		Host: www.kaixin001.com
//		Pragma: no-cache
//		Cookie: _uid=6865223; _sso=; _kxt=0; noMayKnown=1; _laid=0; _email=leeon.liu%40gmail.com; _ref=13b9358e2ef758954403761bc0c2c74e..1376.4d8d6606017ab; _user=143f6a050c998b3d18bb7b49dd41136b_6865223_1301112602; onlinenum=c%3A1; noActivityColor=1; presence=gWMrZE9u2TbcXlpAeu5RRFZ1kUbrfXcNTY1mEg.Njg2NTIyMw; SERVERID=_srv103-49_
//
//		did=41862063&start=&content=%3CP%3E%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%8B%EF%BC%8C%E9%9A%8F%E4%BE%BF%3C%2FP%3E%0D%0A%3CP%3E%3CIMG+alt%3D%22%22+src%3D%22http%3A%2F%2Fbbs.fudan.edu.cn%2Fupload%2FPIC%2F1299922354-1597.jpg%22+border%3D0%3E%3C%2FP%3E%0D%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0D%0A%3CP%3E%E5%86%8D%E5%86%99%E5%87%A0%E8%A1%8C%E5%90%A7%3C%2FP%3E%0D%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0D%0A%3CP%3E%3CFONT+color%3D%23800080+size%3D6%3E%E5%86%8D%E6%9D%A5%E4%B8%AA%E7%89%B9%E5%88%AB%E7%9A%84%3C%2FFONT%3E%3C%2FP%3E
//		&texttype=html&uploadfile=undefined&tags=&draft=0
//		&title=+%E6%B5%8B%E8%AF%95%E6%97%A5%E8%AE%B0
//		&ctimeupdate=1&title2=+%E6%B5%8B%E8%AF%95%E6%97%A5%E8%AE%B0
//		&classid=15411247&privacy=1&passwd=&repaste=1&type=1&sendpasswd=1		

		HttpClient client = HttpConfig.newInstance();
		HttpPost post = new HttpPost(HttpUtil.KAXIN_WWW_DAIRY_WRITE_URL);
		HttpResponse response = null;

//		classid=15411246,privacy=1仅好友可见,title=title2,content,texttype=html
//		repaste=1允许转帖,type=1新闻快讯,sendpassword=1,draft=0非草稿,ctimeupdate=1
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("classid", classid));
		nvps.add(new BasicNameValuePair("title", title));
		nvps.add(new BasicNameValuePair("title2", title));
		nvps.add(new BasicNameValuePair("content", content));
		
		nvps.add(new BasicNameValuePair("privacy", "1"));
		nvps.add(new BasicNameValuePair("texttype", "html"));
		nvps.add(new BasicNameValuePair("repaste", "1"));
		nvps.add(new BasicNameValuePair("type", "1"));
		nvps.add(new BasicNameValuePair("draft", "0"));
		nvps.add(new BasicNameValuePair("sendpassword", "1"));
		nvps.add(new BasicNameValuePair("ctimeupdate", "1"));
		
		HttpUtil.setHeader(post);
		try {
			post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			response = client.execute(post);
			HTTPUtil.consume(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
		
		if (!HTTPUtil.isHttp200(response)) {
			throw new ContentException("post repaste failed");
		}		
	}

	public static void main(String[] args) throws NetworkException {
		Map<String, String> m = prePostDiary("12560532_2538938_1301044192_4380f8867817e34659d1aac7aa80a5ba_kx");
		System.out.println(m);
	}

}
